import { reactive, computed } from 'vue'
import {
  teacherProjectTaskCount,
  studentProjectTaskCount,
} from '@/request/process/layout'
import {getStudentUnreadMsgTotalNum,getStudentUnreadMessageNum,getTeacherUnreadMsgTotalNum,getTeacherUnreadMessageNum} from '@/request/notice/message'

const state = reactive({
  userInfo: JSON.parse(localStorage.getItem('userInfo')) || {},
  messages: {
    任务列表: 0,
    审核成绩: 0,
  },
  studentMessages: {
    任务列表: 0,
  },
  studentNotice:{
    未读总数: 0,
    消息通知: 0,
    系统通知: 0,
  },
  teacherNotice:{
    未读总数: 0,
    消息通知: 0,
    系统通知: 0,
  },
  menusStructure: [
    {
      path: '/teacher',
      label: '首页',
    },
    {
      path: '/projectSquare',
      label: '项目广场',
    },
    {
      path: '/learnSquare',
      label: '学习广场',
    },
  ],

  studentMenusStructure: [
    {
      path: '/stu',
      label: '首页',
    },
    {
      path: '/projectSquare',
      key: 'ProjectSquare',
      label: '项目广场',
    },

    {
      path: '/learnSquare',
      label: '学习广场',
    },
    {
      path: '/teachersAI',
      label: 'AI助手',
    },
  ]
})

// 学生
const isUserLoggedIn = () => !!state.userInfo.token
const userToken = () => state.userInfo.token || ''

const setUserInfo = (newInfo) => {
  state.userInfo = { ...state.userInfo, ...newInfo }
  localStorage.setItem('userInfo', JSON.stringify(state.userInfo))
  // localStorage.setItem('userToken', newInfo?.token || '')
}

const clearUserInfo = () => {
  state.userInfo = {}
  localStorage.removeItem('userInfo')
}

const updateUserToken = (newToken) => {
  setInfo({ token: newToken })
}

const getToken = () => userToken()
const isTeacher = () => state.userInfo?.identity === 'teacher'

// actions
const refreshStudentMessages = async (info) => {
  let res2 = await studentProjectTaskCount({
    search_AND_EQ_projectId: info.id,
    search_AND_EQ_status: 1,
  })
  if (res2) {
    state.studentMessages['任务列表'] = res2.data
  }
}

const refreshMessages = async (info) => {
  let res = {
    任务列表: await teacherProjectTaskCount({
      search_AND_EQ_projectId: info.id,
      search_AND_EQ_status: 1,
    }),
    审核成绩: await teacherProjectTaskCount({
      search_AND_EQ_projectId: info.id,
      search_AND_EQ_status: 2,
    }),
  }
  if (res['任务列表']) state.messages['任务列表'] = res['任务列表'].data
  if (res['审核成绩']) state.messages['审核成绩'] = res['审核成绩'].data
}


const refreshStudentNotice = async (info) => {
  let res1 = await getStudentUnreadMsgTotalNum()
  console.log('res',res1)
  if (res1) {
    state.studentNotice['未读总数'] = res1.data
  }
  let res2 = await getStudentUnreadMessageNum()
  console.log('res',res2)
  if (res2) {
    state.studentNotice['消息通知'] = res2.data['1']
    state.studentNotice['系统通知'] = res2.data['0']
  }
}

const refreshTeacherNotice = async (info) => {
  let res1 = await getTeacherUnreadMsgTotalNum()
  console.log('res',res1)
  if (res1) {
    state.teacherNotice['未读总数'] = res1.data
  }
  let res2 = await getTeacherUnreadMessageNum()
  console.log('res',res2)
  if (res2) {
    state.teacherNotice['消息通知'] = res2.data['1']
    state.teacherNotice['系统通知'] = res2.data['0']
  }
}

export {
  state,
  refreshStudentMessages,
  refreshMessages,
  isUserLoggedIn,
  userToken,
  setUserInfo,
  clearUserInfo,
  updateUserToken,
  getToken,
  isTeacher,

  refreshStudentNotice,
  refreshTeacherNotice
}
